package com.xiashitech.apiservice.api;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.xiashitech.apiservice.Constants.ErrorCode;
import com.xiashitech.apiservice.config.AuthConfig;
import com.xiashitech.apiservice.config.RabbitMqConfig;
import com.xiashitech.apiservice.dao.model.PushHistory;
import com.xiashitech.apiservice.grpc.IncrementalCacheService;
import com.xiashitech.apiservice.service.IPushHistroyService;
import com.xiashitech.apiservice.service.IPushService;
import com.xiashitech.interfaces.apiservice.dto.system.SystemInfoResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@RequestMapping(value = "/v1/agent/pushHistory")
public class PushHistoryController {
    private final IPushService pushService;
    private final AuthConfig authConfig;
    private final IPushHistroyService pushHistroyService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    private final IncrementalCacheService incrementalCacheService;
    private static final Log log = LogFactory.get();
    @RequestMapping(value = "/getHistoryStatusListByIds", method = RequestMethod.POST)
    @ResponseBody
    public List<PushHistory> getHistoryListByIds(@RequestBody List<Integer> ids){
        return pushHistroyService.getHistoryStatusListByIds(ids);
    }
    @RequestMapping(value = "/getHistoryStatusListBySystemIds", method = RequestMethod.POST)
    @ResponseBody
    public List<PushHistory> getHistoryStatusListBySystemId(@RequestBody List<String> systemIds){

        return pushHistroyService.getHistoryStatusListBySystemIds(systemIds);
    }
    @RequestMapping(value = "/updatePushHistoryStatus", method = RequestMethod.POST)
    @ResponseBody
    public SystemInfoResponse updatePushStatus(@RequestBody PushHistory requestPushHistory) {
        PushHistory pushHistory = pushHistroyService.selectOneHistory(requestPushHistory.getId());
        System.out.println("###requestPushHistory "+requestPushHistory.toString());
        if(pushHistory==null)
        {
            return SystemInfoResponse.builder().code(ErrorCode.RESULT_4016.getCode()).message(ErrorCode.RESULT_4016.getMessage()).build();
        }
        if(pushHistory.getStatus()==requestPushHistory.getStatus())
        {
            return SystemInfoResponse.builder().code(ErrorCode.RESULT_1002.getCode()).message(ErrorCode.RESULT_1002.getMessage()).build();
        }
        int i = pushHistroyService.updateOnePushStatus(requestPushHistory.getId(),requestPushHistory.getStatus());
        return SystemInfoResponse.builder().code(ErrorCode.RESULT_1001.getCode()).message(ErrorCode.RESULT_1001.getMessage()).build();

    }
    @RequestMapping(value = "/rabbittest", method = RequestMethod.GET)
    @ResponseBody
    public SystemInfoResponse RabbitTest(HttpServletRequest request){
        //helloSender.send();
        String a = request.getParameter("a")==null?"":request.getParameter("a");
        String context = "";
        String content = "{\n" +
                "    \"pushKey\":\"permissionChange\",\n" +
                "    \"system_id\":\"jsh-erp\",\n" +
                "    \"old\":[\n" +
                "        \"48.jshERP.default.jsh_erp.jsh_account.null.select.0\",\n" +
                "        \"48.jshERP.default.jsh_erp.jsh_account.name.select.1\"\n" +
                "    ],\n" +
                "    \"new\":[\n" +
                "        \"48.jshERP.default.jsh_erp.jsh_account.null.select.1\",\n" +
                "        \"48.jshERP.default.jsh_erp.jsh_account.name.select.1\",\n" +
                "        \"48.jshERP.default.jsh_erp.jsh_account.code.select.1\"\n" +
                "    ]\n" +
                "}";
        String authContent = "{\"pushKey\":\"desensitizationChange\",\"systemId\":\"jsh-erp\",\"change\":[{\"table\":\"jsh-erp-test1.default.jsh_erp.jsh_account\",\"desensitization\":{\"authDesenseSet\":[{\"entityFQN\":\"jsh-erp.default.jsh_erp.jsh_account.name\",\"authDesense\":[{\"apiFQN\":\"ApiServiceTest.jsh-erp-6._account_getAccount\",\"apiPath\":\"/account/getAccount\",\"systemFQN\":\"ApiServiceTest.jsh-erp-6\",\"systemName\":\"jsh-erp-6\",\"patchPath\":\"/data/accountList/*/name\",\"regularCode\":\"111\",\"regular\":\"(.{1}).*\",\"replaceSymbol\":\"$1***\",\"isExecute\":true},{\"apiFQN\":\"ApiService.jsh-erp-6._testGetUserToken\",\"apiPath\":\"/testGetUserToken\",\"systemFQN\":\"ApiService.jsh-erp-6\",\"systemName\":\"jsh-erp-6\",\"patchPath\":\"/username\",\"regularCode\":\"111\",\"regular\":\"(.{1}).*\",\"replaceSymbol\":\"$1***\",\"isExecute\":true},{\"apiFQN\":\"ApiService.jsh-erp-6._testAxis22\",\"apiPath\":\"/testAxis22\",\"systemFQN\":\"ApiService.jsh-erp-6\",\"systemName\":\"jsh-erp-6\",\"patchPath\":\"/username\",\"regularCode\":\"111\",\"regular\":\"(.{1}).*\",\"replaceSymbol\":\"$1***\",\"isExecute\":true},{\"apiFQN\":\"ApiService.jsh-erp-6._testApacheHttp45\",\"apiPath\":\"/testApacheHttp45\",\"systemFQN\":\"ApiService.jsh-erp-6\",\"systemName\":\"jsh-erp-6\",\"patchPath\":\"/username\",\"regularCode\":\"111\",\"regular\":\"(.{1}).*\",\"replaceSymbol\":\"$1***\",\"isExecute\":true},{\"apiFQN\":\"ApiService.jsh-erp-6._userAuthInfo\",\"apiPath\":\"/userAuthInfo\",\"systemFQN\":\"ApiService.jsh-erp-6\",\"systemName\":\"jsh-erp-6\",\"patchPath\":\"/username\",\"regularCode\":\"111\",\"regular\":\"(.{1}).*\",\"replaceSymbol\":\"$1***\",\"isExecute\":true}]}]}}]}";
        if(a.equals("1")){
            context = "Hi, I am message one";
            System.out.println("Sender : " + context);
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME,RabbitMqConfig.DAM_ROUTING_KEY,content);
        }
        else if(a.equals("2")){
            context = "Hi, I am message two";
            System.out.println("Sender : " + context);
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME, RabbitMqConfig.DESENSITIZATION_ROUTING_KEY,authContent);
        }
        else
        {
            context = "boot hello " + new Date();
            System.out.println("Sender : " + context);
            //rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME,"boot.haha", "boot mq hello~~~");
        }
        //new RabbitMqUtil().send("hello");
        return SystemInfoResponse.builder().code("1").message(context).build();
    }

}

